

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh-CN" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh-CN" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>snowboy.snowboydecoder &mdash; wukong-robot 1.2.0 文档</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
        <script type="text/javascript" src="../../_static/translations.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> wukong-robot
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">wukong-robot</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">模块代码</a> &raquo;</li>
        
      <li>snowboy.snowboydecoder</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>snowboy.snowboydecoder 源代码</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>

<span class="kn">import</span> <span class="nn">collections</span>
<span class="kn">import</span> <span class="nn">pyaudio</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">snowboydetect</span>
<span class="kn">from</span> <span class="nn">robot</span> <span class="k">import</span> <span class="n">utils</span><span class="p">,</span> <span class="n">logging</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">wave</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">ctypes</span> <span class="k">import</span> <span class="n">CFUNCTYPE</span><span class="p">,</span> <span class="n">c_char_p</span><span class="p">,</span> <span class="n">c_int</span><span class="p">,</span> <span class="n">cdll</span>
<span class="kn">from</span> <span class="nn">contextlib</span> <span class="k">import</span> <span class="n">contextmanager</span>
<span class="kn">from</span> <span class="nn">robot</span> <span class="k">import</span> <span class="n">constants</span>


<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s2">&quot;snowboy&quot;</span><span class="p">)</span>
<span class="n">TOP_DIR</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>

<span class="n">RESOURCE_FILE</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">TOP_DIR</span><span class="p">,</span> <span class="s2">&quot;resources/common.res&quot;</span><span class="p">)</span>
<span class="n">DETECT_DING</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">TOP_DIR</span><span class="p">,</span> <span class="s2">&quot;resources/ding.wav&quot;</span><span class="p">)</span>
<span class="n">DETECT_DONG</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">TOP_DIR</span><span class="p">,</span> <span class="s2">&quot;resources/dong.wav&quot;</span><span class="p">)</span>

<div class="viewcode-block" id="py_error_handler"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.py_error_handler">[文档]</a><span class="k">def</span> <span class="nf">py_error_handler</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">line</span><span class="p">,</span> <span class="n">function</span><span class="p">,</span> <span class="n">err</span><span class="p">,</span> <span class="n">fmt</span><span class="p">):</span>
    <span class="k">pass</span></div>

<span class="n">ERROR_HANDLER_FUNC</span> <span class="o">=</span> <span class="n">CFUNCTYPE</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="n">c_char_p</span><span class="p">,</span> <span class="n">c_int</span><span class="p">,</span> <span class="n">c_char_p</span><span class="p">,</span> <span class="n">c_int</span><span class="p">,</span> <span class="n">c_char_p</span><span class="p">)</span>

<span class="n">c_error_handler</span> <span class="o">=</span> <span class="n">ERROR_HANDLER_FUNC</span><span class="p">(</span><span class="n">py_error_handler</span><span class="p">)</span>

<div class="viewcode-block" id="no_alsa_error"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.no_alsa_error">[文档]</a><span class="nd">@contextmanager</span>
<span class="k">def</span> <span class="nf">no_alsa_error</span><span class="p">():</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">asound</span> <span class="o">=</span> <span class="n">cdll</span><span class="o">.</span><span class="n">LoadLibrary</span><span class="p">(</span><span class="s1">&#39;libasound.so&#39;</span><span class="p">)</span>
        <span class="n">asound</span><span class="o">.</span><span class="n">snd_lib_error_set_handler</span><span class="p">(</span><span class="n">c_error_handler</span><span class="p">)</span>
        <span class="k">yield</span>
        <span class="n">asound</span><span class="o">.</span><span class="n">snd_lib_error_set_handler</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">yield</span>
        <span class="k">pass</span></div>

<div class="viewcode-block" id="RingBuffer"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.RingBuffer">[文档]</a><span class="k">class</span> <span class="nc">RingBuffer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Ring buffer to hold audio from PortAudio&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">4096</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_buf</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">(</span><span class="n">maxlen</span><span class="o">=</span><span class="n">size</span><span class="p">)</span>

<div class="viewcode-block" id="RingBuffer.extend"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.RingBuffer.extend">[文档]</a>    <span class="k">def</span> <span class="nf">extend</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Adds data to the end of buffer&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_buf</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">data</span><span class="p">)</span></div>

<div class="viewcode-block" id="RingBuffer.get"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.RingBuffer.get">[文档]</a>    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Retrieves data from the beginning of buffer and clears it&quot;&quot;&quot;</span>
        <span class="n">tmp</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">(</span><span class="nb">bytearray</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_buf</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_buf</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">tmp</span></div></div>


<div class="viewcode-block" id="play_audio_file"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.play_audio_file">[文档]</a><span class="k">def</span> <span class="nf">play_audio_file</span><span class="p">(</span><span class="n">fname</span><span class="o">=</span><span class="n">DETECT_DING</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Simple callback function to play a wave file. By default it plays</span>
<span class="sd">    a Ding sound.</span>

<span class="sd">    :param str fname: wave file name</span>
<span class="sd">    :return: None</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">ding_wav</span> <span class="o">=</span> <span class="n">wave</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">fname</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span>
    <span class="n">ding_data</span> <span class="o">=</span> <span class="n">ding_wav</span><span class="o">.</span><span class="n">readframes</span><span class="p">(</span><span class="n">ding_wav</span><span class="o">.</span><span class="n">getnframes</span><span class="p">())</span>
    <span class="k">with</span> <span class="n">no_alsa_error</span><span class="p">():</span>
        <span class="n">audio</span> <span class="o">=</span> <span class="n">pyaudio</span><span class="o">.</span><span class="n">PyAudio</span><span class="p">()</span>
    <span class="n">stream_out</span> <span class="o">=</span> <span class="n">audio</span><span class="o">.</span><span class="n">open</span><span class="p">(</span>
        <span class="nb">format</span><span class="o">=</span><span class="n">audio</span><span class="o">.</span><span class="n">get_format_from_width</span><span class="p">(</span><span class="n">ding_wav</span><span class="o">.</span><span class="n">getsampwidth</span><span class="p">()),</span>
        <span class="n">channels</span><span class="o">=</span><span class="n">ding_wav</span><span class="o">.</span><span class="n">getnchannels</span><span class="p">(),</span>
        <span class="n">rate</span><span class="o">=</span><span class="n">ding_wav</span><span class="o">.</span><span class="n">getframerate</span><span class="p">(),</span> <span class="nb">input</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">output</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">stream_out</span><span class="o">.</span><span class="n">start_stream</span><span class="p">()</span>
    <span class="n">stream_out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">ding_data</span><span class="p">)</span>
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.2</span><span class="p">)</span>
    <span class="n">stream_out</span><span class="o">.</span><span class="n">stop_stream</span><span class="p">()</span>
    <span class="n">stream_out</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="n">audio</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span></div>


<div class="viewcode-block" id="ActiveListener"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.ActiveListener">[文档]</a><span class="k">class</span> <span class="nc">ActiveListener</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Active Listening with VAD &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">decoder_model</span><span class="p">,</span>
                 <span class="n">resource</span><span class="o">=</span><span class="n">RESOURCE_FILE</span><span class="p">):</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;activeListen __init__()&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">recordedData</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">model_str</span> <span class="o">=</span> <span class="s2">&quot;,&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">decoder_model</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">detector</span> <span class="o">=</span> <span class="n">snowboydetect</span><span class="o">.</span><span class="n">SnowboyDetect</span><span class="p">(</span>
            <span class="n">resource_filename</span><span class="o">=</span><span class="n">resource</span><span class="o">.</span><span class="n">encode</span><span class="p">(),</span> <span class="n">model_str</span><span class="o">=</span><span class="n">model_str</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ring_buffer</span> <span class="o">=</span> <span class="n">RingBuffer</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">NumChannels</span><span class="p">()</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">SampleRate</span><span class="p">()</span> <span class="o">*</span> <span class="mi">5</span><span class="p">)</span>

<div class="viewcode-block" id="ActiveListener.listen"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.ActiveListener.listen">[文档]</a>    <span class="k">def</span> <span class="nf">listen</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">interrupt_check</span><span class="o">=</span><span class="k">lambda</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span> <span class="n">sleep_time</span><span class="o">=</span><span class="mf">0.03</span><span class="p">,</span> <span class="n">silent_count_threshold</span><span class="o">=</span><span class="mi">15</span><span class="p">,</span> <span class="n">recording_timeout</span><span class="o">=</span><span class="mi">100</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param interrupt_check: a function that returns True if the main loop</span>
<span class="sd">                                needs to stop.</span>
<span class="sd">        :param silent_count_threshold: indicates how long silence must be heard</span>
<span class="sd">                                       to mark the end of a phrase that is</span>
<span class="sd">                                       being recorded.</span>
<span class="sd">        :param float sleep_time: how much time in second every loop waits.</span>
<span class="sd">        :param recording_timeout: limits the maximum length of a recording.</span>
<span class="sd">        :return: recorded file path</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;activeListen listen()&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="k">def</span> <span class="nf">audio_callback</span><span class="p">(</span><span class="n">in_data</span><span class="p">,</span> <span class="n">frame_count</span><span class="p">,</span> <span class="n">time_info</span><span class="p">,</span> <span class="n">status</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ring_buffer</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">in_data</span><span class="p">)</span>
            <span class="n">play_data</span> <span class="o">=</span> <span class="nb">chr</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">in_data</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">play_data</span><span class="p">,</span> <span class="n">pyaudio</span><span class="o">.</span><span class="n">paContinue</span>

        <span class="k">with</span> <span class="n">no_alsa_error</span><span class="p">():</span>            
            <span class="bp">self</span><span class="o">.</span><span class="n">audio</span> <span class="o">=</span> <span class="n">pyaudio</span><span class="o">.</span><span class="n">PyAudio</span><span class="p">()</span>
        
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;opening audio stream&#39;</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stream_in</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">audio</span><span class="o">.</span><span class="n">open</span><span class="p">(</span>
                <span class="nb">input</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">output</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                <span class="nb">format</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">audio</span><span class="o">.</span><span class="n">get_format_from_width</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">BitsPerSample</span><span class="p">()</span> <span class="o">/</span> <span class="mi">8</span><span class="p">),</span>
                <span class="n">channels</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">NumChannels</span><span class="p">(),</span>
                <span class="n">rate</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">SampleRate</span><span class="p">(),</span>
                <span class="n">frames_per_buffer</span><span class="o">=</span><span class="mi">2048</span><span class="p">,</span>
                <span class="n">stream_callback</span><span class="o">=</span><span class="n">audio_callback</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">critical</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
            <span class="k">return</span> 

        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;audio stream opened&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">interrupt_check</span><span class="p">():</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;detect voice return&quot;</span><span class="p">)</span>
            <span class="k">return</span>

        <span class="n">silentCount</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">recordingCount</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;begin activeListen loop&quot;</span><span class="p">)</span>
        
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>

            <span class="k">if</span> <span class="n">interrupt_check</span><span class="p">():</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;detect voice break&quot;</span><span class="p">)</span>
                <span class="k">break</span>
            <span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ring_buffer</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">sleep_time</span><span class="p">)</span>
                <span class="k">continue</span>
            
            <span class="n">status</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">RunDetection</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">status</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s2">&quot;Error initializing streams or reading audio data&quot;</span><span class="p">)</span>
                
            <span class="n">stopRecording</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">if</span> <span class="n">recordingCount</span> <span class="o">&gt;</span> <span class="n">recording_timeout</span><span class="p">:</span>
                <span class="n">stopRecording</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">elif</span> <span class="n">status</span> <span class="o">==</span> <span class="o">-</span><span class="mi">2</span><span class="p">:</span> <span class="c1">#silence found</span>
                <span class="k">if</span> <span class="n">silentCount</span> <span class="o">&gt;</span> <span class="n">silent_count_threshold</span><span class="p">:</span>
                    <span class="n">stopRecording</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">silentCount</span> <span class="o">=</span> <span class="n">silentCount</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="k">elif</span> <span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="c1">#voice found</span>
                <span class="n">silentCount</span> <span class="o">=</span> <span class="mi">0</span>

            <span class="k">if</span> <span class="n">stopRecording</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">saveMessage</span><span class="p">()</span>

            <span class="n">recordingCount</span> <span class="o">=</span> <span class="n">recordingCount</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">recordedData</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;finished.&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="ActiveListener.saveMessage"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.ActiveListener.saveMessage">[文档]</a>    <span class="k">def</span> <span class="nf">saveMessage</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Save the message stored in self.recordedData to a timestamped file.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">constants</span><span class="o">.</span><span class="n">TEMP_PATH</span><span class="p">,</span> <span class="s1">&#39;output&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()))</span> <span class="o">+</span> <span class="s1">&#39;.wav&#39;</span><span class="p">)</span>
        <span class="n">data</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">recordedData</span><span class="p">)</span>

        <span class="c1">#use wave to save data</span>
        <span class="n">wf</span> <span class="o">=</span> <span class="n">wave</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span>
        <span class="n">wf</span><span class="o">.</span><span class="n">setnchannels</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">NumChannels</span><span class="p">())</span>
        <span class="n">wf</span><span class="o">.</span><span class="n">setsampwidth</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">audio</span><span class="o">.</span><span class="n">get_sample_size</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">audio</span><span class="o">.</span><span class="n">get_format_from_width</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">BitsPerSample</span><span class="p">()</span> <span class="o">/</span> <span class="mi">8</span><span class="p">)))</span>
        <span class="n">wf</span><span class="o">.</span><span class="n">setframerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">SampleRate</span><span class="p">())</span>
        <span class="n">wf</span><span class="o">.</span><span class="n">writeframes</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
        <span class="n">wf</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;finished saving: &quot;</span> <span class="o">+</span> <span class="n">filename</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">stream_in</span><span class="o">.</span><span class="n">stop_stream</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stream_in</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">audio</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span>
        
        <span class="k">return</span> <span class="n">filename</span></div></div>
    
    


<div class="viewcode-block" id="HotwordDetector"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.HotwordDetector">[文档]</a><span class="k">class</span> <span class="nc">HotwordDetector</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Snowboy decoder to detect whether a keyword specified by `decoder_model`</span>
<span class="sd">    exists in a microphone input stream.</span>

<span class="sd">    :param decoder_model: decoder model file path, a string or a list of strings</span>
<span class="sd">    :param resource: resource file path.</span>
<span class="sd">    :param sensitivity: decoder sensitivity, a float of a list of floats.</span>
<span class="sd">                              The bigger the value, the more senstive the</span>
<span class="sd">                              decoder. If an empty list is provided, then the</span>
<span class="sd">                              default sensitivity in the model will be used.</span>
<span class="sd">    :param audio_gain: multiply input volume by this factor.</span>
<span class="sd">    :param apply_frontend: applies the frontend processing algorithm if True.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">decoder_model</span><span class="p">,</span>
                 <span class="n">resource</span><span class="o">=</span><span class="n">RESOURCE_FILE</span><span class="p">,</span>
                 <span class="n">sensitivity</span><span class="o">=</span><span class="p">[],</span>
                 <span class="n">audio_gain</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
                 <span class="n">apply_frontend</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="n">tm</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">decoder_model</span><span class="p">)</span>
        <span class="n">ts</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">sensitivity</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">tm</span> <span class="ow">is</span> <span class="ow">not</span> <span class="nb">list</span><span class="p">:</span>
            <span class="n">decoder_model</span> <span class="o">=</span> <span class="p">[</span><span class="n">decoder_model</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">ts</span> <span class="ow">is</span> <span class="ow">not</span> <span class="nb">list</span><span class="p">:</span>
            <span class="n">sensitivity</span> <span class="o">=</span> <span class="p">[</span><span class="n">sensitivity</span><span class="p">]</span>
        <span class="n">model_str</span> <span class="o">=</span> <span class="s2">&quot;,&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">decoder_model</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">detector</span> <span class="o">=</span> <span class="n">snowboydetect</span><span class="o">.</span><span class="n">SnowboyDetect</span><span class="p">(</span>
            <span class="n">resource_filename</span><span class="o">=</span><span class="n">resource</span><span class="o">.</span><span class="n">encode</span><span class="p">(),</span> <span class="n">model_str</span><span class="o">=</span><span class="n">model_str</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">SetAudioGain</span><span class="p">(</span><span class="n">audio_gain</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">ApplyFrontend</span><span class="p">(</span><span class="n">apply_frontend</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">num_hotwords</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">NumHotwords</span><span class="p">()</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">decoder_model</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">sensitivity</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">sensitivity</span> <span class="o">=</span> <span class="n">sensitivity</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_hotwords</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sensitivity</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_hotwords</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">sensitivity</span><span class="p">),</span> \
                <span class="s2">&quot;number of hotwords in decoder_model (</span><span class="si">%d</span><span class="s2">) and sensitivity &quot;</span> \
                <span class="s2">&quot;(</span><span class="si">%d</span><span class="s2">) does not match&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">num_hotwords</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">sensitivity</span><span class="p">))</span>
        <span class="n">sensitivity_str</span> <span class="o">=</span> <span class="s2">&quot;,&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">t</span><span class="p">)</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">sensitivity</span><span class="p">])</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sensitivity</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">SetSensitivity</span><span class="p">(</span><span class="n">sensitivity_str</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span>        

        <span class="bp">self</span><span class="o">.</span><span class="n">ring_buffer</span> <span class="o">=</span> <span class="n">RingBuffer</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">NumChannels</span><span class="p">()</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">SampleRate</span><span class="p">()</span> <span class="o">*</span> <span class="mi">5</span><span class="p">)</span>

<div class="viewcode-block" id="HotwordDetector.start"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.HotwordDetector.start">[文档]</a>    <span class="k">def</span> <span class="nf">start</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">detected_callback</span><span class="o">=</span><span class="n">play_audio_file</span><span class="p">,</span>
              <span class="n">interrupt_check</span><span class="o">=</span><span class="k">lambda</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
              <span class="n">sleep_time</span><span class="o">=</span><span class="mf">0.03</span><span class="p">,</span>
              <span class="n">audio_recorder_callback</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
              <span class="n">silent_count_threshold</span><span class="o">=</span><span class="mi">15</span><span class="p">,</span>
              <span class="n">recording_timeout</span><span class="o">=</span><span class="mi">100</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Start the voice detector. For every `sleep_time` second it checks the</span>
<span class="sd">        audio buffer for triggering keywords. If detected, then call</span>
<span class="sd">        corresponding function in `detected_callback`, which can be a single</span>
<span class="sd">        function (single model) or a list of callback functions (multiple</span>
<span class="sd">        models). Every loop it also calls `interrupt_check` -- if it returns</span>
<span class="sd">        True, then breaks from the loop and return.</span>

<span class="sd">        :param detected_callback: a function or list of functions. The number of</span>
<span class="sd">                                  items must match the number of models in</span>
<span class="sd">                                  `decoder_model`.</span>
<span class="sd">        :param interrupt_check: a function that returns True if the main loop</span>
<span class="sd">                                needs to stop.</span>
<span class="sd">        :param float sleep_time: how much time in second every loop waits.</span>
<span class="sd">        :param audio_recorder_callback: if specified, this will be called after</span>
<span class="sd">                                        a keyword has been spoken and after the</span>
<span class="sd">                                        phrase immediately after the keyword has</span>
<span class="sd">                                        been recorded. The function will be</span>
<span class="sd">                                        passed the name of the file where the</span>
<span class="sd">                                        phrase was recorded.</span>
<span class="sd">        :param silent_count_threshold: indicates how long silence must be heard</span>
<span class="sd">                                       to mark the end of a phrase that is</span>
<span class="sd">                                       being recorded.</span>
<span class="sd">        :param recording_timeout: limits the maximum length of a recording.</span>
<span class="sd">        :return: None</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="k">def</span> <span class="nf">audio_callback</span><span class="p">(</span><span class="n">in_data</span><span class="p">,</span> <span class="n">frame_count</span><span class="p">,</span> <span class="n">time_info</span><span class="p">,</span> <span class="n">status</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ring_buffer</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">in_data</span><span class="p">)</span>
            <span class="n">play_data</span> <span class="o">=</span> <span class="nb">chr</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">in_data</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">play_data</span><span class="p">,</span> <span class="n">pyaudio</span><span class="o">.</span><span class="n">paContinue</span>

        <span class="k">with</span> <span class="n">no_alsa_error</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">audio</span> <span class="o">=</span> <span class="n">pyaudio</span><span class="o">.</span><span class="n">PyAudio</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stream_in</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">audio</span><span class="o">.</span><span class="n">open</span><span class="p">(</span>
            <span class="nb">input</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">output</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
            <span class="nb">format</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">audio</span><span class="o">.</span><span class="n">get_format_from_width</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">BitsPerSample</span><span class="p">()</span> <span class="o">/</span> <span class="mi">8</span><span class="p">),</span>
            <span class="n">channels</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">NumChannels</span><span class="p">(),</span>
            <span class="n">rate</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">SampleRate</span><span class="p">(),</span>
            <span class="n">frames_per_buffer</span><span class="o">=</span><span class="mi">2048</span><span class="p">,</span>
            <span class="n">stream_callback</span><span class="o">=</span><span class="n">audio_callback</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">interrupt_check</span><span class="p">():</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;detect voice return&quot;</span><span class="p">)</span>
            <span class="k">return</span>

        <span class="n">tc</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">detected_callback</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">tc</span> <span class="ow">is</span> <span class="ow">not</span> <span class="nb">list</span><span class="p">:</span>
            <span class="n">detected_callback</span> <span class="o">=</span> <span class="p">[</span><span class="n">detected_callback</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">detected_callback</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_hotwords</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">detected_callback</span> <span class="o">*=</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_hotwords</span>

        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_hotwords</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">detected_callback</span><span class="p">),</span> \
            <span class="s2">&quot;Error: hotwords in your models (</span><span class="si">%d</span><span class="s2">) do not match the number of &quot;</span> \
            <span class="s2">&quot;callbacks (</span><span class="si">%d</span><span class="s2">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">num_hotwords</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">detected_callback</span><span class="p">))</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;detecting...&quot;</span><span class="p">)</span>

        <span class="n">state</span> <span class="o">=</span> <span class="s2">&quot;PASSIVE&quot;</span>
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>            
            <span class="k">if</span> <span class="n">interrupt_check</span><span class="p">():</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;detect voice break&quot;</span><span class="p">)</span>
                <span class="k">break</span>
            <span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ring_buffer</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">sleep_time</span><span class="p">)</span>
                <span class="k">continue</span>

            <span class="n">status</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">RunDetection</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">status</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s2">&quot;Error initializing streams or reading audio data&quot;</span><span class="p">)</span>

            <span class="c1">#small state machine to handle recording of phrase after keyword</span>
            <span class="k">if</span> <span class="n">state</span> <span class="o">==</span> <span class="s2">&quot;PASSIVE&quot;</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">status</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span> <span class="c1">#key word found</span>

                    <span class="bp">self</span><span class="o">.</span><span class="n">recordedData</span> <span class="o">=</span> <span class="p">[]</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">recordedData</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
                    <span class="n">silentCount</span> <span class="o">=</span> <span class="mi">0</span>
                    <span class="n">recordingCount</span> <span class="o">=</span> <span class="mi">0</span>
                    <span class="n">message</span> <span class="o">=</span> <span class="s2">&quot;Keyword &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">status</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot; detected at time: &quot;</span>
                    <span class="n">message</span> <span class="o">+=</span> <span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y-%m-</span><span class="si">%d</span><span class="s2"> %H:%M:%S&quot;</span><span class="p">,</span>
                                         <span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()))</span>
                    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                    <span class="n">callback</span> <span class="o">=</span> <span class="n">detected_callback</span><span class="p">[</span><span class="n">status</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>                    
                    <span class="k">if</span> <span class="n">callback</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">callback</span><span class="p">()</span>

                    <span class="k">if</span> <span class="n">audio_recorder_callback</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">status</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">utils</span><span class="o">.</span><span class="n">is_proper_time</span><span class="p">():</span>
                        <span class="n">state</span> <span class="o">=</span> <span class="s2">&quot;ACTIVE&quot;</span>
                    <span class="k">continue</span>

            <span class="k">elif</span> <span class="n">state</span> <span class="o">==</span> <span class="s2">&quot;ACTIVE&quot;</span><span class="p">:</span>
                <span class="n">stopRecording</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="k">if</span> <span class="n">recordingCount</span> <span class="o">&gt;</span> <span class="n">recording_timeout</span><span class="p">:</span>
                    <span class="n">stopRecording</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">elif</span> <span class="n">status</span> <span class="o">==</span> <span class="o">-</span><span class="mi">2</span><span class="p">:</span> <span class="c1">#silence found</span>
                    <span class="k">if</span> <span class="n">silentCount</span> <span class="o">&gt;</span> <span class="n">silent_count_threshold</span><span class="p">:</span>
                        <span class="n">stopRecording</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">silentCount</span> <span class="o">=</span> <span class="n">silentCount</span> <span class="o">+</span> <span class="mi">1</span>
                <span class="k">elif</span> <span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="c1">#voice found</span>
                    <span class="n">silentCount</span> <span class="o">=</span> <span class="mi">0</span>

                <span class="k">if</span> <span class="n">stopRecording</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span>
                    <span class="n">fname</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">saveMessage</span><span class="p">()</span>
                    <span class="n">audio_recorder_callback</span><span class="p">(</span><span class="n">fname</span><span class="p">)</span>
                    <span class="n">state</span> <span class="o">=</span> <span class="s2">&quot;PASSIVE&quot;</span>                    
                    <span class="k">continue</span>

                <span class="n">recordingCount</span> <span class="o">=</span> <span class="n">recordingCount</span> <span class="o">+</span> <span class="mi">1</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">recordedData</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>                

        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;finished.&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="HotwordDetector.saveMessage"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.HotwordDetector.saveMessage">[文档]</a>    <span class="k">def</span> <span class="nf">saveMessage</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Save the message stored in self.recordedData to a timestamped file.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">constants</span><span class="o">.</span><span class="n">TEMP_PATH</span><span class="p">,</span> <span class="s1">&#39;output&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()))</span> <span class="o">+</span> <span class="s1">&#39;.wav&#39;</span><span class="p">)</span>
        <span class="n">data</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">recordedData</span><span class="p">)</span>

        <span class="c1">#use wave to save data</span>
        <span class="n">wf</span> <span class="o">=</span> <span class="n">wave</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span>
        <span class="n">wf</span><span class="o">.</span><span class="n">setnchannels</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">NumChannels</span><span class="p">())</span>
        <span class="n">wf</span><span class="o">.</span><span class="n">setsampwidth</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">audio</span><span class="o">.</span><span class="n">get_sample_size</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">audio</span><span class="o">.</span><span class="n">get_format_from_width</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">BitsPerSample</span><span class="p">()</span> <span class="o">/</span> <span class="mi">8</span><span class="p">)))</span>
        <span class="n">wf</span><span class="o">.</span><span class="n">setframerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="o">.</span><span class="n">SampleRate</span><span class="p">())</span>
        <span class="n">wf</span><span class="o">.</span><span class="n">writeframes</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
        <span class="n">wf</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;finished saving: &quot;</span> <span class="o">+</span> <span class="n">filename</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">filename</span></div>

<div class="viewcode-block" id="HotwordDetector.terminate"><a class="viewcode-back" href="../../snowboy.html#snowboy.snowboydecoder.HotwordDetector.terminate">[文档]</a>    <span class="k">def</span> <span class="nf">terminate</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Terminate audio stream. Users can call start() again to detect.</span>
<span class="sd">        :return: None</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_running</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stream_in</span><span class="o">.</span><span class="n">stop_stream</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stream_in</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">audio</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="kc">False</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, wzpan

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>